package GUI;


import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class windowStart extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton 	startButton = new JButton("Carica");
	private JButton 	consultButton = new JButton("Consulta");
	private JButton 	memoriaButton = new JButton("Memoria");
	private ImagePanel Panel_princ;
	private JTextField User_Text_Area;
	private JButton View_PDF;
	private JScrollPane jScrollPaneChat;
	private JButton OkButton;
	private JScrollPane jScrollPaneUser;
	private JTextArea SystemText;
	private JTextArea ChatTextArea;
	private JScrollPane jScrollPaneSystem;
	private JTextArea 	textArea	= new JTextArea("ProlMusic Expert System \n" +
											"to know the music for your state.");
	private String valoreSystem;
	private String valore;
	private String percorso;
	 
	/**
	 */
	public windowStart(){
		
		
		getContentPane().setLayout(null);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLocation	(200,200);
		this.setSize(1051, 639);

		startButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				valoreSystem=new String("carica");
			}
		});
		
		consultButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				valoreSystem=new String("consulta");
			}
		});
		
		memoriaButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				valoreSystem=new String("memoria");
			}
		});

		textArea.setBounds(52, 52, 262, 35);
		consultButton.setBounds(131, 136, 84, 28);
		startButton.setLayout(null);
		startButton.setBounds(15, 134, 70, 28);

		memoriaButton.setLayout(null);
		memoriaButton.setBounds(262, 136, 84, 28);

			{
				Panel_princ = new ImagePanel("image/sfondo.jpg");
				getContentPane().add(Panel_princ);
				Panel_princ.setBounds(4, 0, 1051, 617);
				Panel_princ.setLayout(null);
				Panel_princ.add(textArea);
				{
					jScrollPaneChat = new JScrollPane();
					Panel_princ.add(jScrollPaneChat);
					jScrollPaneChat.setBounds(366, 12, 666, 528);
					{
						ChatTextArea = new JTextArea();
						jScrollPaneChat.setViewportView(ChatTextArea);
						ChatTextArea.setBounds(399, 21, 231, 168);
					}
				}
				{
					jScrollPaneUser = new JScrollPane();
					Panel_princ.add(jScrollPaneUser);
					jScrollPaneUser.setBounds(366, 557, 585, 48);
					{
						User_Text_Area = new JTextField();
						jScrollPaneUser.setViewportView(User_Text_Area);
						User_Text_Area.addActionListener(new ActionListener() {
							
							@Override
							public void actionPerformed(ActionEvent arg0) {
								// TODO Auto-generated method stub
								valore=new String(User_Text_Area.getText());
								ChatTextArea.setText(ChatTextArea.getText()+"Utente:"+valore+"\n");
								User_Text_Area.setText("");
							}
						});
					}
				}
				{
					OkButton = new JButton();
					Panel_princ.add(OkButton);
					OkButton.setText("Ok");
					OkButton.setBounds(984, 557, 56, 49);
					OkButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							valore=new String(User_Text_Area.getText());
							ChatTextArea.setText(ChatTextArea.getText()+"Utente:"+valore+"\n");
							User_Text_Area.setText("");
						}
					});	
				}
				Panel_princ.add(startButton);
				{
					jScrollPaneSystem = new JScrollPane();
					Panel_princ.add(jScrollPaneSystem);
					jScrollPaneSystem.setBounds(10, 229, 333, 376);
					{
						SystemText = new JTextArea();
						jScrollPaneSystem.setViewportView(SystemText);
						SystemText.setBounds(14, 126, 280, 161);
					}
				}
				Panel_princ.add(consultButton);
				Panel_princ.add(memoriaButton);
				{
					View_PDF = new JButton();
					Panel_princ.add(View_PDF);
					View_PDF.setText("Visualizza PDF");
					View_PDF.setBounds(85, 179, 177, 30);
					View_PDF.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							try {
								
								File pdfFile = new File("partiture/"+ percorso + ".pdf");
								if (pdfFile.exists()) {
									
									if (Desktop.isDesktopSupported()) {
										Desktop.getDesktop().open(pdfFile);
									} else {
										System.out.println("Awt Desktop is not supported!");
									}
									
								} else {
									System.out.println("File is not exists!");
								}
								
								System.out.println("Done");
								
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}
					});
				}
			}

		SystemText.setEditable(false);
		ChatTextArea.setEditable(false);
		User_Text_Area.setEditable(false);
		textArea.setEditable(false);
		memoriaButton.setEnabled(false);
		consultButton.setEnabled(false);
		View_PDF.setEnabled(false);
		OkButton.setEnabled(false);
		
		setVisible(true);	
	}
	
	public String getValoreSystem(){
		return valoreSystem;
	}
	
	public void azzeraValoreSystem(){
		valoreSystem=null;
	}
	
	public void modificaStatoServer(String desc){
		SystemText.setText(SystemText.getText()+desc+"\n");
	}
	
	public String getValore(){
		return valore;
	}
	
	public int ottieniValoreNumerico(String val){
		return Integer.parseInt(val);
	}
	
	public void azzeraValore(){
		valore=null;
	}
	
	public void setDomanda(String domanda){
		ChatTextArea.setText(ChatTextArea.getText()+"Sistema:"+domanda+"\n");
	}
	
	public void setRisultato(String[] risultato){
		for(int i=0;i<risultato.length;i++){
			ChatTextArea.setText(ChatTextArea.getText()+"Sistema: Brano Consigliato "+risultato[i]+"\n");
		}
	}
	
	public void setBrani(String[] risultato){
		for(int i=0;i<risultato.length;i++){
			ChatTextArea.setText(ChatTextArea.getText()+"Sistema: Brano "+risultato[i]+"\n");
		}
	}
	
	
	public void setMemoria(String[] elementi){
		for(int i=0;i<elementi.length;i++){
			SystemText.setText(SystemText.getText()+elementi[i]+"\n");
		}
	}
	
	public void setpercorso(String val){
		startButton.setEnabled(true);
		consultButton.setEnabled(true);
		View_PDF.setEnabled(true);
		User_Text_Area.setEditable(false);
		OkButton.setEnabled(false);
		percorso = val;
	}
	
	public void enableConsult(){
		consultButton.setEnabled(true);
	}
	
	public void enableMemoria(){
		startButton.setEnabled(true);
		consultButton.setEnabled(true);
		memoriaButton.setEnabled(true);
		User_Text_Area.setEditable(false);
		OkButton.setEnabled(false);
		
	}
	
	public void enableChat(){
		consultButton.setEnabled(false);
		startButton.setEnabled(false);
		User_Text_Area.setEditable(true);
		OkButton.setEnabled(true);
	}
	
	public void cleanScreen(){
		ChatTextArea.setText("");
		if(SystemText.getText().length()!=0)
			SystemText.setText(SystemText.getText()+"\n");
		View_PDF.setEnabled(false);
		memoriaButton.setEnabled(false);
	}

	
	@SuppressWarnings("serial")
	public class ImagePanel extends JPanel{

	    private BufferedImage image;

	    public ImagePanel(String path) {
	       try {                
	          image = ImageIO.read(new File(path));
	       } catch (IOException ex) {
	            // handle exception...
	       }
	    }

	    @Override
	    public void paintComponent(Graphics g) {
	        g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters

	    }

	}
	
}


